package com.mengtian_tech.tju.dataquery.api.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mengtian_tech.tju.dataquery.api.model.tjuOracle.College;



/**
 * 处理行人门禁数据
 */
@Service
public class CarDataHandleService {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	
	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	StudentService studentService;
	
	
 
	
	/**
	 * 同步数据
	 */
	public synchronized int handle() {
		 
		
		int count = 0;
		
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT * from data_ai.car_park_record where date_on is null limit 0,2000");
		count = list.size();
		
		System.out.println("deal car data, count="+count);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {		
			for(Map<String,Object> item:list) {
				
				Long id = (Long) item.get("id");
				Date inTime = (Date) item.get("IN_TIME");
				String dateOn = sdf.format(inTime);
				
				this.jdbcTemplateDataAi.update("update  data_ai.car_park_record set  date_on=? where id = ?",
						dateOn, id);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		} 
		
		System.out.println("deal car data end , count="+count);
		return count;
		
	}
	
	/**
	 * 处理报表
	 */
	public synchronized Long handleRpt(Long startId) {
		 if(startId == null) startId = 0l;
		 
		 Long lastId = null;
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT id,date_on,in_pass_name from car_park_record where rpt_flag is null and id > ?  and in_pass_name is not null  order by id asc  limit 0,2000",startId);
		
		System.out.println("deal car data rpt, id="+startId);
		
		
		try {		
			for(Map<String,Object> item:list) {
				
				lastId = (Long) item.get("id");
				String date_on = (String) item.get("date_on");
				String in_pass_name = (String) item.get("in_pass_name");
			
				List<Map<String,Object>> 	rptItemList = this.jdbcTemplateDataAi.queryForList(
						"select id,car_count  from rpt_car_park  where date_on = ? and in_pass_name = ? ", 
						date_on,in_pass_name);
				
				
				if(rptItemList.size() == 0) {
					this.jdbcTemplateDataAi.update("insert into rpt_car_park (date_on,in_pass_name,car_count) "
							+ " values(?,?,?)",date_on,in_pass_name,1);
				}else {
					Map<String,Object> rpt = rptItemList.get(0);
					this.jdbcTemplateDataAi.update("update rpt_car_park  set car_count = ? where id = ? ",
						Integer.parseInt(rpt.get("car_count").toString())+1,rpt.get("id"));
				}
				
				
				//更新已处理
				this.jdbcTemplateDataAi.update("update  data_ai.car_park_record set rpt_flag= 1  where id = ?",
						lastId);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		
		return lastId;
		
	}




	/**
	 * 查找用户身份,可以在定时任务中查找
	 */
	public synchronized Long handleUserType(Long startId) {
		 if(startId == null) startId = 0l;
		 
		 Long lastId = null;
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT id,voucher_no from car_park_record where user_type is null and id > ?  and VOUCHER_NO is not null  order by id asc  limit 0,2000",startId);
		
		
		try {		
			for(Map<String,Object> item:list) {
				
				lastId = (Long) item.get("id");
				String plateNo = ((String) item.get("voucher_no")).toUpperCase();
				String userType = "-1";
				String userId = null;
				String userName = null;
				String fkeyId = null;
				College college = null;
				String collegeName = null;
				String collegeId = "-1";
			
				//备案表中查询
				List<Map<String,Object>> carList =  this.jdbcTemplateDataAi.queryForList("select * from car_enter_apply where "
						+ " (cph like ? or cphm like ? or cphm1 like ? or cphm2 like ? or cphm3 like ? or cphm4 like ? or cphm5 like ? ) and apply_status > 0  order by fkey_id desc  LIMIT ?,?",
						"%"+plateNo+"%","%"+plateNo+"%","%"+plateNo+"%","%"+plateNo+"%","%"+plateNo+"%","%"+plateNo+"%","%"+plateNo+"%",0,1);
				if(carList.size() > 0) { //找到车辆备案信息
					Map<String,Object> map = carList.get(0);
					Integer applyType = Integer.parseInt(map.get("apply_type").toString());
					fkeyId = (String) map.get("fkey_id");
					if(applyType.equals(1)) {//大客车
						userType = "1";
						userName = (String) map.get("djfzr");
					}
					
					if(applyType.equals(2)) {//同步学校公车
						userType = "2";
						userName = (String) map.get("xm");
						userId = (String) map.get("gzh");
						
						if(userId != null)college = this.employeeService.getCollege(userId);
					}
					
					if(applyType.equals(3)) {//学生车辆
						userType = "3";
						userName = (String) map.get("xm");
						userId = (String) map.get("xh");
						
						if(userId != null)college = this.studentService.getCollege(userId);
					}
					

					if(applyType.equals(4)) {//教职工车辆
						userType = "4";
						userName = (String) map.get("xm");
						userId = (String) map.get("gzh");
						if(userId != null)college = this.employeeService.getCollege(userId);
					}
					
						
					if(college != null) {
						collegeId = college.getCollegeId();
						collegeName = college.getCollegeName();
					}
					
				}
				
				//访客表中查询
				if("-1".equals(userType)) {
					List<Map<String,Object>> carList2 = this.jdbcTemplateDataAi.queryForList("select * from visit_apply where car_num = ?",plateNo);
					if(carList2.size() > 0) {
						Map<String,Object> map = carList2.get(0);
						fkeyId = map.get("id").toString();
						userName = (String) map.get("name");
						userType = "5"; //访客
						
					}
				}
				
				
				//更新已处理
				this.jdbcTemplateDataAi.update("update  data_ai.car_park_record set user_type = ?,staff_id=?,staff_name=?,fkey_id = ? ,college_id=?,college_name=?  where id = ?",
						userType,userId,userName,fkeyId,collegeId,collegeName,
						lastId);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		
		return lastId;		
	}
	
	
	//处理学院为空的下一个id
	private Long startIdForHandleCollege = 0l;

	/**
	 * 查找用户身份,可以在定时任务中查找
	 */
	public synchronized  List<Map<String,Object>> getNextHandleCollegeList() {
		 
		 
		List<Map<String,Object>> list =  this.jdbcTemplateDataAi.
				queryForList("SELECT id,staff_id,user_type from car_park_record where college_id is null and   staff_id is not null and id > ?   order by id asc  limit 0,2000",startIdForHandleCollege);
		
		if(list.size() > 0) {
			startIdForHandleCollege = new Long( list.get(list.size()-1).get("id").toString());
		}
		
		return list;
		
	}
	
	
	public void updateCollege(String collegeId,String collegeName,Long id) {
		//更新已处理
		this.jdbcTemplateDataAi.update("update  data_ai.car_park_record set college_id=?,college_name=?  where id = ?",
				collegeId,collegeName,id);
	}
}
